﻿
namespace personalplaner.business.extensions
{
	public static class DoubelExtensions
	{
		/// <summary>
		/// Rounds a double to 0.00
		/// </summary>
		/// <param name="value">value to be rounded</param>
		/// <returns>Rounded double</returns>
		public static double Round(this double value)
		{
			return Round(value, 2);
		}

		/// <summary>
		/// Rounds a double to given precision
		/// </summary>
		/// <param name="value">value to be rounded</param>
		/// <param name="precision">Value range from 1-5. Amount of places behinde comma. Eg:1=0.0,2=0.00,3=0.000</param>
		/// <returns>Rounded double</returns>
		public static double Round(this double value, int precision)
		{
			int prec = 10;
			switch (prec)
			{
				case 1:
					prec = 10;
					break;
				case 2:
					prec = 100;
					break;
				case 3:
					prec = 1000;
					break;
				case 4:
					prec = 10000;
					break;
				case 5:
					prec = 100000;
					break;
				default:
					prec = 100;
					break;
			}
			//runden
			int rnd = (int)(value * prec);
			double retval = (double)rnd;
			return retval / prec;
		}
	}
}
